/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;

/*
 
 /de/cs/2on2/ladder/matchmedia_request/save/7707981/
 comment[2507797]
 */

namespace quickReminder
{
    public partial class FormDemoRequest : Form
    {
        private string m_MatchLink;
        PlayerCollection players = new PlayerCollection();
        
        public FormDemoRequest()
        {
            InitializeComponent();
        }
        public void ShowDialog(IWin32Window owner, ListView.ListViewItemCollection items, string matchlink, StringCollection maps)
        {
            m_MatchLink = matchlink;
            foreach(ListViewItem i in items)
            {
                //ListViewItem i2 = new ListViewItem();
                if (i.SubItems[3].Tag != null)
                {
                    players.Add(new Player(i.SubItems[3].Text, (string)i.SubItems[3].Tag, i.SubItems[1].Text));
                    cbPlayer.Items.Add(i.SubItems[3].Text + " (" + i.Text + ")");
                }
                
            }
            if(maps.Count > 0)
            {
                cbRound.Items.Clear();
                foreach (string s in maps)
                {
                    cbRound.Items.Add(s + " T");
                    cbRound.Items.Add(s + " CT");
                }
            }
            ShowDialog(owner);
            //cbPlayer.Focus();
        }

        private void bAdd_Click(object sender, EventArgs e)
        {
            if (cbPlayer.Text != string.Empty && cbRound.Text != string.Empty)
            {
                /*if(RList.Items.Count >= 4)
                {
                    MessageBox.Show("You have already requested the overall maximum of replays!");
                }
                else
                {*/
                /*ListViewItem i = new ListViewItem();
                foreach(ListViewItem item in RList.Items)
                {
                    if (item.Tag == players[cbPlayer.SelectedIndex])
                    {
                        //found existing request
                        
                        i.Tag = players[cbPlayer.SelectedIndex];
                        i.SubItems.Add(cbRound.Text);
                        RList.Items.Insert(item.Index+1,i);
                        return;
                    }
                }
                i.Text = players[cbPlayer.SelectedIndex].Nick;
                i.ImageIndex = 0;
                i.Tag = players[cbPlayer.SelectedIndex];
                i.SubItems.Add(cbRound.Text);
                RList.Items.Add(i);*/


                TreeNode t = new TreeNode(cbRound.Text,1,1);
                foreach(TreeNode tn in RTree.Nodes)
                {
                    if(tn.Tag == players[cbPlayer.SelectedIndex])
                    {
                        tn.Nodes.Add(t);
                        tn.Expand();
                        return;
                    }
                }
                TreeNode player = new TreeNode(players[cbPlayer.SelectedIndex].Nick,0,0);
                player.Tag = players[cbPlayer.SelectedIndex];
                player.Nodes.Add(t);
                RTree.Nodes.Add(player);
                player.Expand();
                    
                //}
            }
            else
                MessageBox.Show(lang.RequestSelectPlayer);
            
        }

        private void bRemove_Click(object sender, EventArgs e)
        {
            TreeNode n = RTree.SelectedNode;
            if(n != null)
            {
                if (n.Level == 0 &&
                    MessageBox.Show(String.Format(lang.ConfirmPlayerDeletion, n.Text), lang.AreYouSure, MessageBoxButtons.YesNo) != DialogResult.Yes)
                        return;
                TreeNode selnode = null;
                if (n.Level > 0)
                {
                    if (n.NextNode != null)
                        selnode = n.NextNode;
                    else if (n.PrevNode != null)
                        selnode = n.PrevNode;
                }
                if (n.Level > 0 && n.Parent.Nodes.Count <= 1)
                    //last item, delete along with parent
                    n.Parent.Remove();
                else
                    //only delete node
                    n.Remove();
                if(selnode != null)
                    RTree.SelectedNode = selnode;
            }
        }

        private void RTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Node.Level == 0)
                e.CancelEdit = true;
        }

        private void cbPlayer_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cbRound.Focus();
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            foreach(TreeNode n in RTree.Nodes)
            {
                string request = quickReminder.Properties.Settings.Default.DemoRequestFormat.Replace("%steamid%", ((Player)n.Tag).SteamID);
                string maps = string.Empty;
                foreach(TreeNode n2 in n.Nodes)
                {
                    if (maps != string.Empty)
                        maps += " " + Properties.Settings.Default.DemoRequestSplitCharacter + " ";
                    maps += n2.Text;
                }
                request = request.Replace("%request%", maps);
                MessageBox.Show(request);
            }
            
            
        }
    }
    public class Player
    {
        public Player(string Nick, string ID, string SteamID)
        {
            this.Nick = Nick;
            this.ID = ID;
            this.SteamID = SteamID;
        }
        public string Nick;
        public string ID;
        public string SteamID;
        bool Barred;
    };

    public class PlayerCollection : CollectionBase
    {
        public int Add(Player item)
        {
            return List.Add(item);
        }
        public void Insert(int index, Player item)
        {
            List.Insert(index, item);
        }
        public void Remove(Player item)
        {
            List.Remove(item);
        }
        public bool Contains(Player item)
        {
            return List.Contains(item);
        }
        public int IndexOf(Player item)
        {
            return List.IndexOf(item);
        }
        public void CopyTo(Player[] array, int index)
        {
            List.CopyTo(array, index);
        }
        public Player this[int index]
        {
            get { return (Player)List[index]; }
            set { List[index] = value; }
        }
    } 
}